<?php

namespace App\Admin\Controllers;

use App\Admin\Metrics\ProjectDesc;
use App\Http\Controllers\Controller;
use Dcat\Admin\Http\Controllers\Dashboard;
use Dcat\Admin\Layout\Column;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Grid;
use App\Models\Project;
use App\Models\ProjectUser;
use App\Models\Cabinet;
use App\Models\CabinetAlarmLog;
use App\Handlers\WeconHandler;

use App\Models\Device;
use App\Models\DeviceLog;
use Dcat\Admin\Widgets\Card;
use Dcat\Admin\Widgets\Box;
use Dcat\Admin\Admin;
use App\Models\Task;


class ProjectDescController extends Controller
{
    public function index(Content $content){
                Admin::script(
<<<JS
    // let menu = document.getElementsByClassName("main-menu").length
    // if(menu<=0){
    //     location.reload();
    // };
    $(".navbar-header").next().show()
    $("body").removeClass('sidebar-collapse')
JS
);

        return $content
            ->header('首页大数据')
            ->description('')
            ->body(function (Row $row) {
                $row->column(12, function (Column $column) {
                    $column->row(function(Row $row) {
                        $row->column(0, $this->deviceInfo());
                        $row->column(0, $this->alertInfo());
                        // $row->column(0, $this->taskInfo());
                    });


                    $column->row(function(Row $row) {
                        $row->column(3, function (Column $column) {
                            $column->row(new ProjectDesc\DeviceCard());
                            // $column->row(new ProjectDesc\DeviceCollection());
                        });
                        $row->column(6, $this->map());
                        $row->column(3, function (Column $column) {
                            $column->row(new ProjectDesc\DeviceAlertCard());
                            $column->row($this->alertLogInfo());
                        });
                    });
                });

            });
    }

    /**
     * [deviceInfo 设备概况信息表格]
     * @return [type] [description]
     */
    protected function deviceInfo(){
        $projects = ProjectUser::where('user_id',Admin::user()->id)->pluck('projectId');
        $boxes = Cabinet::whereIn('project_id',$projects)->where('pid', 0)->pluck('machine_code', 'id')->toArray();
        $total = count($boxes);
        $online = 0;
        $offline = 0;
        if ($boxes) {
            foreach ($boxes as $id => $machineCode) {
                $check = WeconHandler::getBoxStatus($machineCode);
                if ($check) {
                    $online++;
                } else {
                    $offline++;
                }
            }
        }
        $data['total'] = $total;
        $data['online'] = $online;
        $data['offline'] = $offline;
        $card = Card::make(view('admin.ProjectDesc.deviceInfo', $data));
        return $card;
    }

    /**
     * [alertInfo 报警信息概况信息表格]
     * @return [type] [description]
     */
    protected function alertInfo(){
        $projects = ProjectUser::where('user_id',Admin::user()->id)->pluck('projectId');

        $total = CabinetAlarmLog::whereIn('project_id',$projects)->count();
        $deal = CabinetAlarmLog::whereIn('project_id',$projects)->where('status', CabinetAlarmLog::STATUS_YES)->count();

        $data['total'] = $total;
        $data['deal'] = $deal;
        $data['undeal'] = $total - $deal;
        $card = Card::make(view('admin.ProjectDesc.alertInfo', $data));
        return $card;
    }

    /**
     * [taskInfo 任务概况信息表格]
     * @return [type] [description]
     */
    protected function taskInfo(){
        $projectId = session('now_project');
        $adminModel = config('admin.database.users_model');
        $admin = new $adminModel();

        $devices = Device::where('projectId', $projectId)->pluck('deviceId')->toArray();
        $data['projectPeople'] = $adminModel::where('projectId', $projectId)->count();
        $data['total'] = Task::where('projectId', $projectId)->count();
        $data['undeal'] = Task::where('projectId', $projectId)->where('status', 0)->count();
        $card = Card::make(view('admin.ProjectDesc.taskInfo', $data));
        return $card;
    }

    /**
     * [alertLogInfo 设备报警记录概况表格]
     * @return [type] [description]
     */
    protected function alertLogInfo(){
        $projects = ProjectUser::where('user_id',Admin::user()->id)->pluck('projectId');

        $list = CabinetAlarmLog::select('alarm_name', 'created_at')->whereIn('project_id',$projects)->orderBy('created_at', 'desc')->take(10)->get()->toArray();
        foreach ($list as &$one) {
            $one['created_at'] = $one['created_at'];
            $one['msg'] = $one['alarm_name'];
        }

        $data['list'] = $list;
        $card = Card::make(view('admin.ProjectDesc.alertLogInfo', $data));
        return $card;
    }

    /**
     * [map 地图]
     * @return [type] [description]
     */
    protected function map(){
        $projectIds = ProjectUser::where('user_id', Admin::user()->id)->pluck('projectId')->toArray();
        $projects = Project::select(['projectId','projectname', 'lat', 'lng'])->whereIn('projectId', $projectIds)->get()->toArray();
        $list = [];
        foreach ($projects as $one) {
            if (!empty($one['lat'])  && !empty($one['lng'])) {
                $t['name'] = $one['projectname'];
                $t['lat'] = $one['lat'];
                $t['lng'] = $one['lng'];
                $t['id'] = $one['projectId'];

                $undealLogNum = CabinetAlarmLog::where('project_id', $one['projectId'])->where('status', CabinetAlarmLog::STATUS_NO)->count();
                if ($undealLogNum > 0) {
                    $t['icon'] = url('icon/warning.png');
                } else {
                    $t['icon'] = '';
                }
                $boxId = Cabinet::where('project_id', $one['projectId'])->where('pid', 0)->value('id');
                $t['url'] = $boxId ? admin_url('cabinet-children').'/'.$boxId : '';
                array_push($list, $t);
            }
        }
        $data['list'] = $list;
        $card = Card::make(view('admin.ProjectDesc.map', $data));
        return $card;
    }
}
